/*
 * comm_wifi.h
 *
 *  Created on: Oct 15, 2021
 *      Author: Administrator
 */
#ifndef COMM_WIFI_H
#define COMM_WIFI_H
#include "sc95f.h"
#include "machine_stat.h"
#include "string.h"
#include "protocal.h"

#define wait_receive      0
#define uart_send         1
#define start_wifi        2
#define server_end_wifi   3
#define comm_enjoin       4
#define wifi_reset        5

typedef enum{
   WIFI_STA_IDLE,
   WIFI_STA_LINKING,
   WIFI_STA_LINK_OK,
   WIFI_STA_ROUTE,
   WIFI_STA_SERVER,        	
   WIFI_STA_SUCCESS,
   WIFI_STA_DISLINK, 
   WIFI_STA_DISLINK_WAIT,	
}WifiState_t;

enum{
   WIFI_CMD_TIMESYNC = 0x11,    
};

//#define WIFI_WAIT_TIME         20
#define WIFI_WAIT_TIME_RETURN  20
#define WIFI_WAIT_TIME_RETURN_STRAT_WIFI  200
#define WIFI_START_INTERVAL_TIME          200
#define WIFI_SMARTLINK_TIMEOUT_CNT        60    /* 2 minutes*/

#define WIFI_UNLINK_RETRY_TIMEOUT         (200)
#define WIFI_UNLINK_RETRY_COUNT           (3)

#define WIFI_RES_TIME          0
#define WIFI_SEND_TIME         0X02
#define WIFI_RETURN_TIMES      0X03
#define WIFI_RETURN_RES        0X00
/////////////////////////////
#define send_none         0
#define send_ack          1
#define send_set          2
#define send_res          3
#define send_get          4
#define send_nak          5
#define send_inf          6
#define send_f0_00           7
#define send_f0_01           8
#define send_f1_00           9
#define send_f2_00           10
#define send_f2_01           11
#define send_f3_01           12
#define send_f4              13
#define send_f3_00           14
#define send_e0_00           15
/////////////////////////////
#define receive_f0        0xf0
#define receive_e0        0xe0
#define receive_f1        0xf1
#define receive_f3        0xf3
#define receive_f2        0xf2
#define receive_f4        0xf4
#define receive_10        0x10
#define receive_03        0x03

///////// 10: wifi配网相关(WIFI CMD) /////////
#define WCMD_WIFI_VERSION       0x00
#define WCMD_RADIO_WAVE         0x01
#define WCMD_ROUTE_STATUS       0x02
#define WCMD_ROUTE_INIT         0x03
#define WCMD_ROUTE_INIT_OK      0x04
#define WCMD_SERVER_STATUS      0x05
#define route_ssid              0x06
#define route_key               0x07
#define WCMD_LINK_START         0x08
#define WCMD_LINK_END           0x09
#define WCMD_UNLINK_START       0x0a
#define WCMD_UNLINK_OK          0x0b
      



/////////////////////////////
#define SEND_F0_DATA      0xf0
#define SEND_F1_DATA      0xf1
#define SEND_F3_00_DATA      0xf3
#define NO_SEND_DATA      0X00

#define MCU_SEND_WIFI_TIME_5MIN        300

#define F0_00_DATA_LENGTH   55
#define F0_01_DATA_LENGTH   55
#define F1_00_DATA_LENGTH   40
#define F2_00_DATA_LENGTH   55
#define F2_01_DATA_LENGTH   55
#define F3_00_DATA_LENGTH   55
#define F4_00_DATA_LENGTH   55
#define FE_00_DATA_LENGTH   55

#define WIFI_STXdata 0
#define WIFI_SORTdata 1
#define WIFI_LENGTHdata 2
#define WIFI_COMMANDdata 3
#define WIFI_TEXTdata 4
#define WIFI_ETXdata 5
#define WIFI_BCCdata 6



#define stx_date            0x02
#define sort_date           0x09
#define etx_date            0x03

#define WIFI_TEXT_SET        0x53
#define WIFI_TEXT_GET        0x47
#define WIFI_TEXT_RES        0x52
#define WIFI_TEXT_INF        0x49
#define WIFI_TEXT_ACK        0x41
#define WIFI_TEXT_NAK        0x4E

#define WIFI_NLOAD           P51
#define WIFI_Reset(val)      P50=val

#define CMD_SOFTWEAR_VER      0x10

#define LOW_L                1
#define HIGH_H               0

#define WIFI_COMM_PEROID_LIMIT            100//59//250
#define TEXT_SET        0x53
#define TEXT_GET        0x47
#define TEXT_RES        0x52
#define TEXT_INF        0x49
#define TEXT_ACK        0x41
#define TEXT_NAK        0x4E

typedef enum
{
    LINK_FAILURE=0,
    LINK_SUCCESS,
}LINK_STAT;

typedef struct 
{
    uint8_t wifi_cmd; //大函数控制
    LINK_STAT smartlink; //08 09
    //LINK_STAT smart_link_stat;
    LINK_STAT wifi_server; 
    LINK_STAT wifi_luyouqi;
    LINK_STAT success_link_wifi; //只代表以前连接过，不代表当前连接成功，作用：灯的闪烁，快闪慢闪，不参与数据处理
    LINK_STAT pass_send_data;//温度，灯状态等数据发送是否成功，start—wifi等命令发送成功和这个变量无关
    uint8_t   Send_wifi_data;//记录重发
    uint8_t   wifi_state ; //aaron.0:init;1:have start,receive 0x09;
}WIFI_STAT1;

typedef enum{
   RUN_MODE_NORMAL,
   RUN_MODE_KITCHEN,
   RUN_MODE_SHOWER,    
}RunMode_t;

typedef enum{
    SM_LINK_00,
    SM_LINK_10,
    SM_LINK_10_1,
    SM_LINK_11,
    SM_LINK_20,
    SM_LINK_30,
    SM_LINK_31,
    SM_LINK_40,
    SM_LINK_40_1,
}linkSm_t;

enum{
	RECV_ROUTE_FAIL,
	RECV_ROUTE_OK,
	RECV_SERVE_FAIL,
	RECV_SERVE_OK,
	RECV_UNLINK_START,
	RECV_UNLINK_OK,
	RECV_SMARTLINK_START,
	RECV_SMARTLINK_OK,
};

typedef enum
{
    
    SEND2_END=0,
    SEND2_RUN
    
}SEND2_FLAG;
typedef struct 
{
    SEND2_FLAG send_flag;
    uint8_t * pdata1;
    uint8_t pdata_len;
    uint8_t send_len;
}UART2_SEND;
extern UART2_SEND uart2_send_flag;
//extern USCI2_HandleInfoDef USCI2_HANDLE_USCI2;
extern WIFI_STAT1 wifi_stat;
extern uint8_t wifi_send_flag;
extern uint8_t wifi_return_send;
extern uint8_t wifi_class_data;

extern WifiState_t WifiState;
//extern uint8_t WifiVersion[];

#define IsReceivedCmd(cmd)   (WifiCom->RecvBuff[PACK_CMD_OFFSET]==cmd)
 
uint8_t wifi_comm_process(uint8_t cmd);
uint8_t check_f1_data(uint8_t cmd_new);
 
uint8_t UART2_IT_SEND_FUNCTION(UART2_SEND *wifi_uart_send);

uint8_t UART2_IT_SEND_START_function(UART2_SEND *wifi_uart_send,uint8_t *bpata,uint8_t bpata_len);


uint8_t WIFI_IsIdle(void);
uint8_t WIFI_IsConnected(void);

void WIFI_Init(void);
void WIFI_Task(void);
void WIFI_LinkFsm(void);
void WIFI_ReceiveFunc(Comm_t *com);
#endif
